﻿#region License
/*Copyright (c) 2010 Andrew Collard (andrewcollard@hotmail.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/
#endregion

using System;
using System.IO;
using System.Text;

namespace TorchView.Game
{
  /// <summary>
  /// A Torchlight character
  /// </summary>
  public sealed class Character
  {
    const uint CHEATER_FLAG_FALSE = 78;
    const uint CHEATER_FLAG_TRUE = 214;
    const uint SPELL_SLOT_COUNT = 4;

    public byte Unknown1 { get; set; }
    public long CurrentModelID { get; set; }  //Class/parent/pet current model ID.
    public long OriginModelID { get; set; }      //original model ID, in case of transformation
    public long CharacterID { get; set; }   //uuid of this specific character instance.
    public bool NPC { get; set; }
    public byte Unknown3 { get; set; }
    public byte Unknown4 { get; set; }
    public bool Champion { get; set; }
    public bool UsingAltWeapon { get; set; }
    public bool Cheater { get; set; }
    public float TransformTimer { get; set; }
    public int Unknown6 { get; set; }
    public int Unknown7 { get; set; }
    public int Unknown8 { get; set; }
    public float Scale { get; set; }
    public long Master { get; set; }
    public long Unknown9 { get; set; }
    public int Unknown10 { get; set; }
    public string Name { get; set; }
    public int CurrentFloor { get; set; }
    public int DeepestFloor { get; set; }
    public Vector Position { get; set; }
    public Matrix Orientation { get; set; }
    public uint PlayerLevel { get; set; }
    public uint Experience { get; set; }
    public uint FameLevel { get; set; }
    public uint Fame { get; set; }
    public float CurrentHealth { get; set; }
    public uint BaseHealth { get; set; }
    public uint BonusHealth { get; set; }
    public float CurrentMana { get; set; }
    public uint BaseMana { get; set; }
    public int BonusMana { get; set; }
    public int Unknown14 { get; set; }
    public int Unknown15 { get; set; }
    public TimeSpan TimePlayed { get; set; }
    public float Unknown17 { get; set; }
    public int RemainingStatPoints { get; set; }
    public int RemainingSkillPoints { get; set; }
    public int Unknown18 { get; set; }
    public long RightMouseSkill { get; set; }
    public long LeftMouseSkill { get; set; }
    public long TabSkill { get; set; }
    public uint[] SkillLevels { get; private set; }
    public Spell[] Spells { get; set; }
    public ResistBonus BonusResistance { get; set; }
    public int Strength { get; set; }
    public int Defense { get; set; }
    public int Dexterity { get; set; }
    public int Magic { get; set; }
    public int Gold { get; set; }
    public int Unknown20 { get; set; }
    public ulong Unknown21 { get; set; }
    public int Unknown22 { get; set; }
    public string[] Mods { get; private set; }
    public Item[] Items { get; set; }
    public Effect[] PassiveEffects { get; set; }
    public Effect[] UseEffects { get; set; }
    public Effect[] TargetEffects { get; set; }

    public static Character Read(BinaryReader reader)
    {
      Character character = new Character();

      ReadFirstBlock(character, reader);
      ReadSecondBlock(character, reader);
      ReadThirdBlock(character, reader);
      ReadFourthBlock(character, reader);

      return character;
    }

    /// <summary>
    /// This block contains the class, difficulty, hardcore, etc...
    /// </summary>
    private static void ReadFirstBlock(Character character, BinaryReader reader)
    {
      character.Unknown1 = reader.ReadByte();
      character.CurrentModelID = reader.ReadInt64();
      character.OriginModelID = reader.ReadInt64();
      character.CharacterID = reader.ReadInt64();
      character.NPC = reader.ReadBoolean();
      character.Unknown3 = reader.ReadByte();
      character.Unknown4 = reader.ReadByte();
      character.Champion = reader.ReadBoolean();
      character.UsingAltWeapon = reader.ReadBoolean();
      character.Cheater = (reader.ReadByte() == CHEATER_FLAG_TRUE);
      character.TransformTimer = reader.ReadSingle();
      character.Unknown6 = reader.ReadInt32();
      character.Unknown7 = reader.ReadInt32();
      character.Unknown8 = reader.ReadInt32();
      character.Scale = reader.ReadSingle();
      character.Master = reader.ReadInt64();
      character.Unknown9 = reader.ReadInt64();
    }

    /// <summary>
    /// This block contains the character name, XP, health, etc...
    /// </summary>
    private static void ReadSecondBlock(Character character, BinaryReader reader)
    {
      character.Unknown10 = reader.ReadInt32();
      character.Name = SVTHelper.ReadString(reader);
      character.CurrentFloor = reader.ReadInt32();
      character.DeepestFloor = reader.ReadInt32();
      character.Position = Vector.Read(reader);
      character.Orientation = Matrix.Read(reader);
      character.PlayerLevel = reader.ReadUInt32();
      character.Experience = reader.ReadUInt32();
      character.FameLevel = reader.ReadUInt32();
      character.Fame = reader.ReadUInt32();
      character.CurrentHealth = reader.ReadSingle();
      character.BaseHealth = reader.ReadUInt32();
      character.BonusHealth = reader.ReadUInt32();
      character.CurrentMana = reader.ReadSingle();
      character.BaseMana = reader.ReadUInt32();

      character.BonusMana = reader.ReadInt32();
      character.Unknown14 = reader.ReadInt32();
      character.Unknown15 = reader.ReadInt32();
      character.TimePlayed = Time.Read(reader);
      character.Unknown17 = reader.ReadSingle();

      character.RemainingStatPoints = reader.ReadInt32();
      character.RemainingSkillPoints = reader.ReadInt32();

      character.Unknown18 = reader.ReadInt32();

      character.RightMouseSkill = reader.ReadInt64();
      character.TabSkill = reader.ReadInt64();
      character.LeftMouseSkill = reader.ReadInt64();
    }

    /// <summary>
    /// This block contains the attributes like strength, dexterity, etc...
    /// </summary>
    private static void ReadThirdBlock(Character character, BinaryReader reader)
    {
      uint skillSpellCount = reader.ReadUInt32();
      uint skillCount = skillSpellCount;

      ReadSkills(character, reader, skillCount);
      ReadSpells(character, reader);

      character.BonusResistance = ResistBonus.Read(reader);

      character.Strength = reader.ReadInt32();
      character.Dexterity = reader.ReadInt32();
      character.Defense = reader.ReadInt32();
      character.Magic = reader.ReadInt32();
      character.Gold = reader.ReadInt32();
      character.Unknown20 = reader.ReadInt32();
      character.Unknown21 = reader.ReadUInt64();
      character.Unknown22 = reader.ReadInt32();
    }

    private static void ReadSkills(Character character, BinaryReader reader, uint skillCount)
    {
      uint[] skills = new uint[skillCount];
      for (int i = 0; i < skillCount; i++)
        skills[i] = reader.ReadUInt32();
      character.SkillLevels = skills;
    }

    private static void ReadSpells(Character character, BinaryReader reader)
    {
      Spell[] spells = new Spell[SPELL_SLOT_COUNT];
      for (int i = 0; i < SPELL_SLOT_COUNT; i++)
        spells[i] = new Spell { Name = SVTHelper.ReadString(reader), Level = reader.ReadUInt32() };
      character.Spells = spells;
    }

    private static void ReadFourthBlock(Character character, BinaryReader reader)
    {
      ReadMods(character, reader);
      ReadItems(character, reader);

      character.PassiveEffects = ReadEffects(reader);
      character.UseEffects = ReadEffects(reader);
      character.TargetEffects = ReadEffects(reader);
    }

    private static void ReadMods(Character character, BinaryReader reader)
    {
      int modCount = reader.ReadInt32();
      if (modCount < 0)
        modCount = 0;
      string[] mods = new string[modCount];
      for (int i = 0; i < modCount; i++)
        mods[i] = SVTHelper.ReadString(reader);
      character.Mods = mods;
    }

    private static void ReadItems(Character character, BinaryReader reader)
    {
      int itemCount = reader.ReadInt32();
      if (itemCount < 0)
        itemCount = 0;
      Item[] items = new Item[itemCount];
      for (int i = 0; i < itemCount; i++)
        items[i] = Item.Read(reader);
      character.Items = items;
    }

    private static Effect[] ReadEffects(BinaryReader reader)
    {
      var effectCount = reader.ReadInt32();
      if (effectCount < 0)
        effectCount = 0;
      Effect[] effects = new Effect[effectCount];
      for (int i = 0; i < effectCount; i++)
        effects[i] = Effect.Read(reader);
      return effects;
    }

    public override string ToString()
    {
      return "Character: " + Name;
    }
  }
}
